/***********************************************************************
** Copyright (C) 2011 Anthony Naddeo <anthony.naddeo@gmail.com>
**
** This file is part of LinkedUp
**
** LinkedUp is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** LinkedUp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this package; If not, see <http://www.gnu.org/licenses/>.
**
***********************************************************************/
import QtQuick 1.1
import com.meego 1.0
import "../components"

MyPage {
	id: root
	anchors.fill: parent
	title: searchSheet.param || "Search"

	onGetFinished: list.content.model.xml = responseText
	Component.onCompleted: searchSheet.open()

	function search(type, param, start,count){
		if(type === "People"){
			list.pageQuery = "/people-search/people"
			list.contentModel = peopleModel
			list.contentDelegate = peopleDelegate
			get(API.search_person(param,start,count))
		}else if(type === "Companies"){
			list.pageQuery = "/company-search/companies"
			list.contentModel = companyModel
			list.contentDelegate = companyDelegate
			get(API.search_company(param,start,count))
		}
		else if(type === "Jobs"){
			list.pageQuery = "/job-search/jobs"
			list.contentModel = jobModel
			list.contentDelegate = jobDelegate
			get(API.search_job(param,start,count))
		}
	}

	tools: ToolBarLayout {
		ToolIcon { platformIconId: "toolbar-view-menu";
			anchors.right: parent===undefined ? undefined : parent.right
			onClicked: (myMenu.status == DialogStatus.Closed) ? myMenu.open() : myMenu.close()
		}
		ToolIcon { platformIconId: "toolbar-back";
			anchors.left: parent===undefined ? undefined : parent.left
			visible: pageStack ? pageStack.depth > 1 ? true : false : false
			onClicked: {
				if(pageStack.depth > 1)
					pageStack.pop()
			}
		}
		ToolIcon{
			platformIconId: "toolbar-search"
			anchors.centerIn: parent
			onClicked: searchSheet.status == DialogStatus.Open ? searchSheet.close() : searchSheet.open()
		}

	}

	LinkedInSearchSheet{
		id: searchSheet;
		z:1000;
		visualParent: root
		onSearch: {
			list.start = "0"
			root.search(type, param, list.start, list.count )
		}
	}


	PagedList{
		id: list
		anchors.fill: root
		z: 100
		anchors.topMargin: borderHeight
		contentModel: peopleModel
		contentDelegate: peopleDelegate
		Connections{
			onStartChanged: root.search(searchSheet.type, searchSheet.param, list.start, list.count)
		}
	}

	PeopleModel{id:peopleModel}
	JobModel{id:jobModel}
	CompanyModel{id:companyModel}

	Component{
		id:peopleDelegate
		PeopleDelegate{}
	}
	Component{
		id: jobDelegate
		JobDelegate{}
	}
	Component{
		id: companyDelegate
		CompanyDelegate{}
	}

}
